package com.hxzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxzy.common.util.WaimaiThreadLocal;
import com.hxzy.entity.ShoppingCart;
import com.hxzy.mapper.ShoppingCartMapper;
import com.hxzy.service.ShoppingCartService;
import com.hxzy.vo.LoginVO;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 购物车
 * @author Administrator
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    /**
     * 查询某个商家的用户购物车信息
     * @param merchantId
     * @return
     */
    @Override
    public List<ShoppingCart> findUserCartByMerchandId(Long merchantId) {
        LoginVO loginVO= WaimaiThreadLocal.getCurrent();

        QueryWrapper<ShoppingCart> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("merchant_id",merchantId);
        queryWrapper.eq("user_id",loginVO.getAccountVO().getId());
        List<ShoppingCart> shoppingCarts = this.list(queryWrapper);
        return shoppingCarts;
    }

    @Override
    public int addCart(ShoppingCart shoppingCart) {

          /* 第一次  {amount: 26, dishId: 1, merchantId: 1, name: "宫保鸡丁", image: null}
             第二次  {amount: 26, dishId: 1, merchantId: 1, name: "宫保鸡丁", image: null}
           */
        LoginVO loginVO= WaimaiThreadLocal.getCurrent();
        shoppingCart.setUserId(loginVO.getAccountVO().getId());
        //先查询购物车中有没有相同的商品(商品ID,用户ID)
        ShoppingCart db=this.baseMapper.findByUserIdAndDishId(loginVO.getAccountVO().getId(),shoppingCart.getDishId());
        int number=0;
        //第一次添加
        if(db==null){
            //数量为1
            shoppingCart.setUserId(loginVO.getAccountVO().getId());
            shoppingCart.setNumber(1);
            number=shoppingCart.getNumber();
            this.save(shoppingCart);
        }else{
            db.setNumber(db.getNumber()+1);
            number=db.getNumber();
            this.updateById(db);
        }
        return  number;
    }

    /**
     * 减少购物车库存
     * @param dishId
     * @return
     */
    @Override
    public int subCart(Long dishId) {
        LoginVO loginVO= WaimaiThreadLocal.getCurrent();
        //先查询购物车中有没有相同的商品(商品ID,用户ID)
        ShoppingCart db=this.baseMapper.findByUserIdAndDishId(loginVO.getAccountVO().getId(),dishId);
        if(db==null){
            return 0;
        }

        //得到购物车的数量
        int num=db.getNumber();

        if(num==1){
            //删除购物车的数量
            this.removeById(db.getId());
            return 0;
        }else{
            num=num-1;
            //更新购物车的数量
            this.baseMapper.updateCartNumber(db.getId(),num);
        }
        return num;
    }

    /**
     * 清除当前商家的购物车
     * @param merchantId
     */
    @Override
    public void clean(Long merchantId) {
        LoginVO loginVO= WaimaiThreadLocal.getCurrent();
        this.baseMapper.clean(loginVO.getAccountVO().getId(),merchantId);
    }


}
